/*     */ package com.zimbra.cs.account.accesscontrol;
/*     */ 
/*     */ import com.zimbra.common.account.Key.DistributionListBy;
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.DistributionList;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.DynamicGroup;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Group;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.ldap.LdapDIT;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.ldap.IAttributes;
/*     */ import com.zimbra.cs.ldap.LdapTODO.ACLTODO;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor;
/*     */ import com.zimbra.cs.ldap.ZLdapFilter;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.io.PrintStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ @LdapTODO.ACLTODO
/*     */ public class CollectAllEffectiveRights
/*     */ {
/*     */   private final RightBearer mRightBearer;
/*     */   private RightBearer.Grantee mGrantee;
/*     */   private final boolean mExpandSetAttrs;
/*     */   private final boolean mExpandGetAttrs;
/*     */   private final RightCommand.AllEffectiveRights mResult;
/*     */   private final LdapProv mProv;
/*     */   
/*     */   public static class GroupShape
/*     */   {
/*  74 */     Set<String> mGroups = new HashSet();
/*  75 */     Set<String> mMembers = new HashSet();
/*     */     
/*     */     public String toString()
/*     */     {
/*  79 */       StringBuilder sb = new StringBuilder();
/*  80 */       for (String group : this.mGroups) {
/*  81 */         sb.append("group " + group + "\n");
/*     */       }
/*  83 */       for (String member : this.mMembers) {
/*  84 */         sb.append("    member " + member + "\n");
/*     */       }
/*  86 */       return sb.toString();
/*     */     }
/*     */     
/*     */     private static void debug(String msg, Set<GroupShape> shapes) {
/*  90 */       if (ZimbraLog.acl.isDebugEnabled()) {
/*  91 */         for (GroupShape shape : shapes) {
/*  92 */           ZimbraLog.acl.debug(msg + "\n" + shape.toString());
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     private void addGroups(Set<String> groups) {
/*  98 */       this.mGroups.addAll(groups);
/*     */     }
/*     */     
/*     */     private void addGroup(String group) {
/* 102 */       this.mGroups.add(group);
/*     */     }
/*     */     
/*     */     public Set<String> getGroups() {
/* 106 */       return this.mGroups;
/*     */     }
/*     */     
/*     */     private void addMembers(Set<String> members) {
/* 110 */       this.mMembers.addAll(members);
/*     */     }
/*     */     
/*     */     private void addMember(String member) {
/* 114 */       this.mMembers.add(member);
/*     */     }
/*     */     
/*     */     public Set<String> getMembers() {
/* 118 */       return this.mMembers;
/*     */     }
/*     */     
/*     */     private boolean removeMemberIfPresent(String member) {
/* 122 */       if (this.mMembers.contains(member)) {
/* 123 */         this.mMembers.remove(member);
/* 124 */         return true;
/*     */       }
/* 126 */       return false;
/*     */     }
/*     */     
/*     */     private boolean hasMembers() {
/* 130 */       return !this.mMembers.isEmpty();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     public static void shapeMembers(TargetType targetType, Set<GroupShape> shapes, CollectAllEffectiveRights.AllGroupMembers group)
/*     */       throws ServiceException
/*     */     {
/* 142 */       Set<GroupShape> newShapes = new HashSet();
/*     */       
/*     */ 
/*     */ 
/* 146 */       GroupShape newShape = new GroupShape();
/* 147 */       newShape.addGroup(group.getGroupName());
/* 148 */       newShape.addMembers(group.getMembers(targetType));
/*     */       
/* 150 */       for (GroupShape shape : shapes)
/*     */       {
/*     */ 
/* 153 */         GroupShape intersectShape = new GroupShape();
/*     */         
/* 155 */         for (String member : group.getMembers(targetType)) {
/* 156 */           if (shape.removeMemberIfPresent(member)) {
/* 157 */             intersectShape.addMember(member);
/* 158 */             newShape.removeMemberIfPresent(member);
/*     */           }
/*     */         }
/*     */         
/* 162 */         if (intersectShape.hasMembers())
/*     */         {
/*     */ 
/*     */ 
/* 166 */           intersectShape.addGroups(shape.getGroups());
/* 167 */           intersectShape.addGroup(group.getGroupName());
/*     */           
/*     */ 
/* 170 */           newShapes.add(intersectShape);
/*     */         }
/*     */       }
/*     */       
/*     */ 
/*     */ 
/* 176 */       shapes.addAll(newShapes);
/*     */       
/*     */ 
/*     */ 
/* 180 */       if (newShape.hasMembers()) {
/* 181 */         shapes.add(newShape);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static class AllGroupMembers
/*     */   {
/*     */     String mGroupName;
/* 189 */     Set<String> mAccounts = new HashSet();
/* 190 */     Set<String> mCalendarResources = new HashSet();
/* 191 */     Set<String> mDistributionLists = new HashSet();
/*     */     
/*     */     AllGroupMembers(String groupName) {
/* 194 */       this.mGroupName = groupName;
/*     */     }
/*     */     
/*     */     String getGroupName() {
/* 198 */       return this.mGroupName;
/*     */     }
/*     */     
/*     */     Set<String> getMembers(TargetType targetType) throws ServiceException {
/* 202 */       switch (CollectAllEffectiveRights.1.$SwitchMap$com$zimbra$cs$account$accesscontrol$TargetType[targetType.ordinal()]) {
/*     */       case 1: 
/* 204 */         return this.mAccounts;
/*     */       case 2: 
/* 206 */         return this.mCalendarResources;
/*     */       case 3: 
/* 208 */         return this.mDistributionLists;
/*     */       }
/* 210 */       throw ServiceException.FAILURE("internal error", null);
/*     */     }
/*     */   }
/*     */   
/*     */   private static class Visitor
/*     */     extends SearchLdapOptions.SearchLdapVisitor
/*     */   {
/*     */     private final LdapProv mProv;
/*     */     private final LdapDIT mLdapDIT;
/* 219 */     private final Set<String> mNames = new HashSet();
/*     */     
/*     */     Visitor(LdapProv prov) {
/* 222 */       super();
/* 223 */       this.mProv = prov;
/* 224 */       this.mLdapDIT = this.mProv.getDIT();
/*     */     }
/*     */     
/*     */     public void visit(String dn, IAttributes ldapAttrs)
/*     */     {
/*     */       try {
/* 230 */         String name = this.mLdapDIT.dnToEmail(dn, ldapAttrs);
/* 231 */         this.mNames.add(name);
/*     */       } catch (ServiceException e) {
/* 233 */         ZimbraLog.acl.warn("cannot get name from dn [" + dn + "]", e);
/*     */       }
/*     */     }
/*     */     
/*     */     private Set<String> getResult() {
/* 238 */       return this.mNames;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static void getAllEffectiveRights(RightBearer rightBearer, boolean expandSetAttrs, boolean expandGetAttrs, RightCommand.AllEffectiveRights result)
/*     */     throws ServiceException
/*     */   {
/* 254 */     CollectAllEffectiveRights caer = new CollectAllEffectiveRights(rightBearer, expandSetAttrs, expandGetAttrs, result);
/*     */     
/* 256 */     caer.collect();
/*     */   }
/*     */   
/*     */ 
/*     */   public CollectAllEffectiveRights(RightBearer rightBearer, boolean expandSetAttrs, boolean expandGetAttrs, RightCommand.AllEffectiveRights result)
/*     */     throws ServiceException
/*     */   {
/* 263 */     this.mRightBearer = rightBearer;
/*     */     
/* 265 */     if ((this.mRightBearer instanceof RightBearer.Grantee)) {
/* 266 */       this.mGrantee = ((RightBearer.Grantee)this.mRightBearer);
/*     */     }
/*     */     
/* 269 */     this.mExpandSetAttrs = expandSetAttrs;
/* 270 */     this.mExpandGetAttrs = expandGetAttrs;
/* 271 */     this.mResult = result;
/*     */     
/* 273 */     this.mProv = LdapProv.getInst();
/*     */   }
/*     */   
/*     */   private void collect() throws ServiceException
/*     */   {
/* 278 */     if ((this.mRightBearer instanceof RightBearer.GlobalAdmin)) {
/* 279 */       for (TargetType tt : TargetType.values()) {
/* 280 */         RightCommand.EffectiveRights er = new RightCommand.EffectiveRights(tt.getCode(), null, null, this.mRightBearer.getId(), this.mRightBearer.getName());
/*     */         
/*     */         Entry target;
/*     */         
/*     */         Entry target;
/* 285 */         if (TargetType.config == tt) {
/* 286 */           target = this.mProv.getConfig(); } else { Entry target;
/* 287 */           if (TargetType.global == tt) {
/* 288 */             target = this.mProv.getGlobalGrant();
/*     */           } else
/* 290 */             target = PseudoTarget.createPseudoTarget(this.mProv, tt, null, null, true, null, null, null);
/*     */         }
/* 292 */         CollectEffectiveRights.getEffectiveRights(this.mRightBearer, target, tt, this.mExpandSetAttrs, this.mExpandGetAttrs, er);
/* 293 */         this.mResult.setAll(tt, er);
/*     */       }
/* 295 */       return;
/*     */     }
/*     */     
/*     */ 
/* 299 */     Set<TargetType> targetTypesToSearch = new HashSet(Arrays.asList(TargetType.values()));
/*     */     
/*     */ 
/* 302 */     Set<String> granteeIdsToSearch = this.mGrantee.getIdAndGroupIds();
/*     */     
/*     */ 
/* 305 */     if (this.mGrantee.isAccount()) {
/* 306 */       Domain domain = this.mProv.getDomain(this.mGrantee.getAccount());
/* 307 */       granteeIdsToSearch.add(ZimbraACE.ExternalGroupInfo.encode(domain.getId(), ""));
/*     */     }
/*     */     
/* 310 */     SearchGrants searchGrants = new SearchGrants(this.mProv, targetTypesToSearch, granteeIdsToSearch);
/* 311 */     Set<SearchGrants.GrantsOnTarget> grantsOnTargets = searchGrants.doSearch().getResults(true);
/*     */     
/*     */ 
/* 314 */     Set<Group> groupsWithGrants = new HashSet();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 324 */     for (SearchGrants.GrantsOnTarget grantsOnTarget : grantsOnTargets) {
/* 325 */       Entry grantedOnEntry = grantsOnTarget.getTargetEntry();
/* 326 */       ZimbraACL acl = grantsOnTarget.getAcl();
/* 327 */       TargetType targetType = TargetType.getTargetType(grantedOnEntry);
/*     */       
/* 329 */       if (targetType == TargetType.global) {
/* 330 */         computeRightsInheritedFromGlobalGrant();
/* 331 */       } else if (targetType == TargetType.domain) {
/* 332 */         computeRightsInheritedFromDomain((Domain)grantedOnEntry);
/* 333 */         computeSubDomainRightsInheritedFromDomain(acl, (Domain)grantedOnEntry);
/* 334 */       } else if (targetType == TargetType.dl) {
/* 335 */         groupsWithGrants.add((DistributionList)grantedOnEntry);
/* 336 */       } else if (targetType == TargetType.group) {
/* 337 */         groupsWithGrants.add((DynamicGroup)grantedOnEntry);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 385 */     Set<String> processedGroups = new HashSet();
/*     */     
/* 387 */     Set<GroupShape> accountShapes = new HashSet();
/* 388 */     Set<GroupShape> calendarResourceShapes = new HashSet();
/* 389 */     Set<GroupShape> distributionListShapes = new HashSet();
/*     */     
/* 391 */     for (Group group : groupsWithGrants) {
/* 392 */       String groupName = group.getName().toLowerCase();
/* 393 */       if (!processedGroups.contains(groupName))
/*     */       {
/*     */ 
/* 396 */         processedGroups.add(groupName);
/*     */         
/*     */ 
/* 399 */         AllGroupMembers allMembers = getAllGroupMembers(group);
/* 400 */         GroupShape.shapeMembers(TargetType.account, accountShapes, allMembers);
/* 401 */         GroupShape.shapeMembers(TargetType.calresource, calendarResourceShapes, allMembers);
/* 402 */         GroupShape.shapeMembers(TargetType.dl, distributionListShapes, allMembers);
/*     */         
/*     */ 
/*     */ 
/*     */ 
/* 407 */         processedGroups.add(group.getId());
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 416 */         for (String nestedGoupMember : allMembers.getMembers(TargetType.dl)) {
/* 417 */           String nestedGoupMemberName = nestedGoupMember.toLowerCase();
/* 418 */           if (!processedGroups.contains(nestedGoupMemberName))
/*     */           {
/*     */ 
/* 421 */             processedGroups.add(nestedGoupMemberName);
/*     */             
/*     */ 
/* 424 */             DistributionList subDl = this.mProv.get(Key.DistributionListBy.name, nestedGoupMemberName);
/*     */             
/* 426 */             if (subDl != null) {
/* 427 */               AllGroupMembers allMembersOfSubDl = getAllGroupMembers(subDl);
/* 428 */               GroupShape.shapeMembers(TargetType.account, accountShapes, allMembersOfSubDl);
/* 429 */               GroupShape.shapeMembers(TargetType.calresource, calendarResourceShapes, allMembersOfSubDl);
/* 430 */               GroupShape.shapeMembers(TargetType.dl, distributionListShapes, allMembersOfSubDl);
/*     */             }
/*     */           }
/*     */         }
/*     */       } }
/* 435 */     if (ZimbraLog.acl.isDebugEnabled()) {
/* 436 */       GroupShape.debug("accountShapes", accountShapes);
/* 437 */       GroupShape.debug("calendarResourceShapes", calendarResourceShapes);
/* 438 */       GroupShape.debug("distributionListShapes", distributionListShapes);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 444 */     Set<String> entryIdsHasGrants = new HashSet();
/* 445 */     for (SearchGrants.GrantsOnTarget grantsOnTarget : grantsOnTargets) {
/* 446 */       Entry grantedOnEntry = grantsOnTarget.getTargetEntry();
/* 447 */       if ((grantedOnEntry instanceof NamedEntry)) {
/* 448 */         entryIdsHasGrants.add(((NamedEntry)grantedOnEntry).getId());
/*     */       }
/*     */     }
/*     */     
/* 452 */     computeRightsOnGroupShape(TargetType.account, accountShapes, entryIdsHasGrants);
/* 453 */     computeRightsOnGroupShape(TargetType.calresource, calendarResourceShapes, entryIdsHasGrants);
/* 454 */     computeRightsOnGroupShape(TargetType.dl, distributionListShapes, entryIdsHasGrants);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 461 */     for (SearchGrants.GrantsOnTarget grantsOnTarget : grantsOnTargets) {
/* 462 */       Entry grantedOnEntry = grantsOnTarget.getTargetEntry();
/* 463 */       ZimbraACL acl = grantsOnTarget.getAcl();
/* 464 */       TargetType targetType = TargetType.getTargetType(grantedOnEntry);
/*     */       
/* 466 */       if (targetType != TargetType.global) {
/* 467 */         computeRightsOnEntry(targetType, grantedOnEntry);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public AllGroupMembers getAllGroupMembers(Group group)
/*     */     throws ServiceException
/*     */   {
/* 480 */     Set<String> allGroups = getAllGroups();
/* 481 */     Set<String> allCalendarResources = getAllCalendarResources();
/*     */     
/* 483 */     AllGroupMembers allMembers = new AllGroupMembers(group.getName());
/* 484 */     getAllGroupMembers(group, allGroups, allCalendarResources, allMembers);
/*     */     
/* 486 */     return allMembers;
/*     */   }
/*     */   
/*     */ 
/*     */   private void getAllGroupMembers(Group group, Set<String> allGroups, Set<String> allCalendarResources, AllGroupMembers result)
/*     */     throws ServiceException
/*     */   {
/* 493 */     Set<String> members = group.getAllMembersSet();
/* 494 */     Set<String> accountMembers = new HashSet(members);
/*     */     
/*     */ 
/* 497 */     for (String member : members)
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/* 502 */       if (allGroups.contains(member))
/*     */       {
/* 504 */         accountMembers.remove(member);
/*     */         
/*     */ 
/* 507 */         if (!result.getMembers(TargetType.dl).contains(member)) {
/* 508 */           result.getMembers(TargetType.dl).add(member);
/* 509 */           DistributionList grp = this.mProv.get(Key.DistributionListBy.name, member);
/* 510 */           if (grp != null)
/*     */           {
/* 512 */             getAllGroupMembers(grp, allGroups, allCalendarResources, result);
/*     */           }
/*     */         }
/* 515 */       } else if (allCalendarResources.contains(member)) {
/* 516 */         accountMembers.remove(member);
/* 517 */         result.getMembers(TargetType.calresource).add(member);
/*     */       }
/*     */     }
/* 520 */     result.getMembers(TargetType.account).addAll(accountMembers);
/*     */   }
/*     */   
/*     */   private Set<String> getAllGroups() throws ServiceException {
/* 524 */     LdapDIT ldapDIT = this.mProv.getDIT();
/* 525 */     String base = ldapDIT.mailBranchBaseDN();
/* 526 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().allGroups();
/*     */     
/*     */ 
/* 529 */     String[] returnAttrs = { "cn", "uid" };
/*     */     
/* 531 */     Visitor visitor = new Visitor(this.mProv);
/* 532 */     this.mProv.searchLdapOnMaster(base, filter, returnAttrs, visitor);
/* 533 */     return visitor.getResult();
/*     */   }
/*     */   
/*     */   private Set<String> getAllCalendarResources() throws ServiceException {
/* 537 */     LdapDIT ldapDIT = this.mProv.getDIT();
/* 538 */     String base = ldapDIT.mailBranchBaseDN();
/* 539 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().allCalendarResources();
/*     */     
/*     */ 
/* 542 */     String[] returnAttrs = { "cn", "uid" };
/*     */     
/* 544 */     Visitor visitor = new Visitor(this.mProv);
/* 545 */     this.mProv.searchLdapOnMaster(base, filter, returnAttrs, visitor);
/* 546 */     return visitor.getResult();
/*     */   }
/*     */   
/*     */   private void computeRightsInheritedFromGlobalGrant() throws ServiceException
/*     */   {
/* 551 */     for (TargetType tt : ) { Entry targetEntry;
/*     */       Entry targetEntry;
/* 553 */       if (tt == TargetType.global) {
/* 554 */         targetEntry = this.mProv.getGlobalGrant(); } else { Entry targetEntry;
/* 555 */         if (tt == TargetType.config) {
/* 556 */           targetEntry = this.mProv.getConfig();
/*     */         } else {
/* 558 */           targetEntry = PseudoTarget.createPseudoTarget(this.mProv, tt, null, null, true, null, null);
/*     */         }
/*     */       }
/* 561 */       RightCommand.EffectiveRights er = new RightCommand.EffectiveRights(tt.getCode(), TargetType.getId(targetEntry), targetEntry.getLabel(), this.mGrantee.getId(), this.mGrantee.getName());
/*     */       
/*     */ 
/*     */ 
/* 565 */       CollectEffectiveRights.getEffectiveRights(this.mGrantee, targetEntry, this.mExpandSetAttrs, this.mExpandGetAttrs, er);
/*     */       
/* 567 */       this.mResult.setAll(tt, er);
/*     */     }
/*     */   }
/*     */   
/*     */   private void computeRightsInheritedFromDomain(Domain grantedOnDomain) throws ServiceException
/*     */   {
/* 573 */     computeRightsInheritedFromDomain(TargetType.account, grantedOnDomain);
/*     */     
/* 575 */     computeRightsInheritedFromDomain(TargetType.calresource, grantedOnDomain);
/*     */     
/* 577 */     computeRightsInheritedFromDomain(TargetType.dl, grantedOnDomain);
/*     */     
/* 579 */     computeRightsInheritedFromDomain(TargetType.group, grantedOnDomain);
/*     */   }
/*     */   
/*     */   private void computeRightsInheritedFromDomain(TargetType targetType, Domain grantedOnDomain)
/*     */     throws ServiceException
/*     */   {
/* 585 */     String domainId = TargetType.getId(grantedOnDomain);
/* 586 */     String domainName = grantedOnDomain.getLabel();
/*     */     
/*     */ 
/* 589 */     Entry pseudoTarget = PseudoTarget.createPseudoTarget(this.mProv, targetType, Key.DomainBy.id, grantedOnDomain.getId(), false, null, null);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 594 */     RightCommand.EffectiveRights er = new RightCommand.EffectiveRights(targetType.getCode(), TargetType.getId(pseudoTarget), pseudoTarget.getLabel(), this.mGrantee.getId(), this.mGrantee.getName());
/*     */     
/*     */ 
/* 597 */     CollectEffectiveRights.getEffectiveRights(this.mGrantee, pseudoTarget, this.mExpandSetAttrs, this.mExpandGetAttrs, er);
/*     */     
/*     */ 
/* 600 */     this.mResult.addDomainEntry(targetType, domainName, er);
/*     */   }
/*     */   
/*     */   private void computeSubDomainRightsInheritedFromDomain(ZimbraACL acl, Domain grantedOnDomain)
/*     */     throws ServiceException
/*     */   {
/* 606 */     boolean noSubDomainGrants = acl.getSubDomainACEs().isEmpty();
/* 607 */     if (noSubDomainGrants) {
/* 608 */       return;
/*     */     }
/*     */     
/*     */ 
/* 612 */     List<Domain> subDomains = searchSubDomains(grantedOnDomain);
/*     */     
/* 614 */     for (Domain subDomain : subDomains) {
/* 615 */       String targetId = subDomain.getId();
/* 616 */       String targetName = subDomain.getName();
/*     */       
/* 618 */       RightCommand.EffectiveRights er = new RightCommand.EffectiveRights(TargetType.domain.getCode(), targetId, targetName, this.mGrantee.getId(), this.mGrantee.getName());
/*     */       
/*     */ 
/*     */ 
/* 622 */       CollectEffectiveRights.getEffectiveRights(this.mGrantee, subDomain, this.mExpandSetAttrs, this.mExpandGetAttrs, er);
/*     */       
/* 624 */       this.mResult.addEntry(TargetType.domain, targetName, er);
/*     */     }
/*     */   }
/*     */   
/*     */   private List<Domain> searchSubDomains(Domain domain) throws ServiceException
/*     */   {
/* 630 */     List<Domain> subDomains = new ArrayList();
/*     */     
/* 632 */     String base = this.mProv.getDIT().domainNameToDN(domain.getName());
/* 633 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().allDomains();
/* 634 */     String[] returnAttrs = { "zimbraId" };
/* 635 */     SearchSubDomainVisitor visitor = new SearchSubDomainVisitor(null);
/*     */     
/* 637 */     this.mProv.searchLdapOnMaster(base, filter, returnAttrs, visitor);
/*     */     
/* 639 */     List<String> zimbraIds = visitor.getResults();
/* 640 */     for (String zimbraId : zimbraIds) {
/* 641 */       if (!zimbraId.equalsIgnoreCase(domain.getId()))
/*     */       {
/*     */ 
/*     */         try
/*     */         {
/*     */ 
/* 647 */           Domain subDomain = (Domain)TargetType.lookupTarget(this.mProv, TargetType.domain, TargetBy.id, zimbraId);
/*     */           
/* 649 */           subDomains.add(subDomain);
/*     */         } catch (ServiceException e) {
/* 651 */           ZimbraLog.acl.warn("canot find domain by id " + zimbraId, e);
/*     */         }
/*     */       }
/*     */     }
/* 655 */     return subDomains;
/*     */   }
/*     */   
/*     */   private static class SearchSubDomainVisitor extends SearchLdapOptions.SearchLdapVisitor {
/* 659 */     List<String> mDomainIds = new ArrayList();
/*     */     
/*     */     public void visit(String dn, Map<String, Object> attrs, IAttributes ldapAttrs)
/*     */     {
/* 663 */       String zimbraId = (String)attrs.get("zimbraId");
/* 664 */       if (zimbraId != null) {
/* 665 */         this.mDomainIds.add(zimbraId);
/*     */       }
/*     */     }
/*     */     
/*     */     private List<String> getResults() {
/* 670 */       return this.mDomainIds;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void computeRightsOnGroupShape(TargetType targetType, Set<GroupShape> groupShapes, Set<String> entryIdsHasGrants)
/*     */     throws ServiceException
/*     */   {
/* 697 */     for (GroupShape shape : groupShapes)
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 710 */       Entry target = null;
/* 711 */       RightCommand.EffectiveRights er = null;
/* 712 */       for (String memberName : shape.getMembers()) {
/* 713 */         target = TargetType.lookupTarget(this.mProv, targetType, TargetBy.name, memberName, false);
/* 714 */         if (target != null) {
/* 715 */           String targetId = TargetType.getId(target);
/* 716 */           if (!entryIdsHasGrants.contains(targetId)) {
/* 717 */             er = new RightCommand.EffectiveRights(targetType.getCode(), targetId, target.getLabel(), this.mGrantee.getId(), this.mGrantee.getName());
/*     */             
/*     */ 
/* 720 */             CollectEffectiveRights.getEffectiveRights(this.mGrantee, target, this.mExpandSetAttrs, this.mExpandGetAttrs, er);
/*     */             
/* 722 */             break;
/*     */           }
/*     */         }
/*     */       }
/*     */       
/* 727 */       if (er != null) {
/* 728 */         this.mResult.addAggregation(targetType, shape.getMembers(), er);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void computeRightsOnEntry(TargetType grantedOnTargetType, Entry grantedOnEntry) throws ServiceException
/*     */   {
/* 735 */     String targetId = TargetType.getId(grantedOnEntry);
/* 736 */     String targetName = grantedOnEntry.getLabel();
/*     */     
/* 738 */     RightCommand.EffectiveRights er = new RightCommand.EffectiveRights(grantedOnTargetType.getCode(), targetId, targetName, this.mGrantee.getId(), this.mGrantee.getName());
/*     */     
/*     */ 
/*     */ 
/* 742 */     CollectEffectiveRights.getEffectiveRights(this.mGrantee, grantedOnEntry, this.mExpandSetAttrs, this.mExpandGetAttrs, er);
/*     */     
/* 744 */     this.mResult.addEntry(grantedOnTargetType, targetName, er);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private static AllGroupMembers allGroupMembers(DistributionList dl)
/*     */     throws ServiceException
/*     */   {
/* 753 */     CollectAllEffectiveRights caer = new CollectAllEffectiveRights(null, false, false, null);
/* 754 */     return caer.getAllGroupMembers(dl);
/*     */   }
/*     */   
/*     */   private static void groupTest() throws ServiceException {
/* 758 */     Provisioning prov = Provisioning.getInstance();
/* 759 */     DistributionList dl = prov.get(Key.DistributionListBy.name, "group1@phoebe.mac");
/*     */     
/* 761 */     AllGroupMembers allMembers = allGroupMembers(dl);
/*     */     
/* 763 */     System.out.println("\naccounts");
/* 764 */     for (String member : allMembers.getMembers(TargetType.account)) {
/* 765 */       System.out.println("  " + member);
/*     */     }
/* 767 */     System.out.println("\ncalendar resources");
/* 768 */     for (String member : allMembers.getMembers(TargetType.calresource)) {
/* 769 */       System.out.println("  " + member);
/*     */     }
/* 771 */     System.out.println("\ngroups");
/* 772 */     for (String member : allMembers.getMembers(TargetType.dl))
/* 773 */       System.out.println("  " + member);
/*     */   }
/*     */   
/*     */   private static void setupShapeTest1() throws ServiceException {
/* 777 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/* 780 */     String domainName = "test.com";
/* 781 */     Domain domain = prov.createDomain(domainName, new HashMap());
/*     */     
/* 783 */     DistributionList groupA = prov.createDistributionList("groupA@" + domainName, new HashMap());
/* 784 */     DistributionList groupB = prov.createDistributionList("groupB@" + domainName, new HashMap());
/* 785 */     DistributionList groupC = prov.createDistributionList("groupC@" + domainName, new HashMap());
/* 786 */     DistributionList groupD = prov.createDistributionList("groupD@" + domainName, new HashMap());
/*     */     
/* 788 */     String pw = "test123";
/* 789 */     Account A = prov.createAccount("A@" + domainName, pw, null);
/* 790 */     Account B = prov.createAccount("B@" + domainName, pw, null);
/* 791 */     Account C = prov.createAccount("C@" + domainName, pw, null);
/* 792 */     Account D = prov.createAccount("D@" + domainName, pw, null);
/* 793 */     Account AB = prov.createAccount("AB@" + domainName, pw, null);
/* 794 */     Account AC = prov.createAccount("AC@" + domainName, pw, null);
/* 795 */     Account AD = prov.createAccount("AD@" + domainName, pw, null);
/* 796 */     Account BC = prov.createAccount("BC@" + domainName, pw, null);
/* 797 */     Account BD = prov.createAccount("BD@" + domainName, pw, null);
/* 798 */     Account CD = prov.createAccount("CD@" + domainName, pw, null);
/* 799 */     Account ABC = prov.createAccount("ABC@" + domainName, pw, null);
/* 800 */     Account ABD = prov.createAccount("ABD@" + domainName, pw, null);
/* 801 */     Account ACD = prov.createAccount("ACD@" + domainName, pw, null);
/* 802 */     Account BCD = prov.createAccount("BCD@" + domainName, pw, null);
/* 803 */     Account ABCD = prov.createAccount("ABCD@" + domainName, pw, null);
/*     */     
/* 805 */     groupA.addMembers(new String[] { A.getName(), AB.getName(), AC.getName(), AD.getName(), ABC.getName(), ABD.getName(), ACD.getName(), ABCD.getName() });
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 810 */     groupB.addMembers(new String[] { B.getName(), AB.getName(), BC.getName(), BD.getName(), ABC.getName(), ABD.getName(), BCD.getName(), ABCD.getName() });
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 815 */     groupC.addMembers(new String[] { C.getName(), AC.getName(), BC.getName(), CD.getName(), ABC.getName(), ACD.getName(), BCD.getName(), ABCD.getName() });
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 820 */     groupD.addMembers(new String[] { D.getName(), AD.getName(), BD.getName(), CD.getName(), ABD.getName(), ACD.getName(), BCD.getName(), ABCD.getName() });
/*     */   }
/*     */   
/*     */ 
/*     */   private static void shapeTest1()
/*     */     throws ServiceException
/*     */   {
/* 827 */     setupShapeTest1();
/*     */     
/* 829 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/* 832 */     Set<DistributionList> groupsWithGrants = new HashSet();
/* 833 */     String domainName = "test.com";
/* 834 */     groupsWithGrants.add(prov.get(Key.DistributionListBy.name, "groupA@" + domainName));
/* 835 */     groupsWithGrants.add(prov.get(Key.DistributionListBy.name, "groupB@" + domainName));
/* 836 */     groupsWithGrants.add(prov.get(Key.DistributionListBy.name, "groupC@" + domainName));
/* 837 */     groupsWithGrants.add(prov.get(Key.DistributionListBy.name, "groupD@" + domainName));
/*     */     
/* 839 */     Set<GroupShape> accountShapes = new HashSet();
/* 840 */     Set<GroupShape> calendarResourceShapes = new HashSet();
/* 841 */     Set<GroupShape> distributionListShapes = new HashSet();
/*     */     
/* 843 */     for (DistributionList group : groupsWithGrants)
/*     */     {
/*     */ 
/* 846 */       DistributionList dl = prov.get(Key.DistributionListBy.id, group.getId());
/* 847 */       AllGroupMembers allMembers = allGroupMembers(dl);
/* 848 */       GroupShape.shapeMembers(TargetType.account, accountShapes, allMembers);
/* 849 */       GroupShape.shapeMembers(TargetType.calresource, calendarResourceShapes, allMembers);
/* 850 */       GroupShape.shapeMembers(TargetType.dl, distributionListShapes, allMembers);
/*     */     }
/*     */     
/* 853 */     int count = 1;
/* 854 */     for (GroupShape shape : accountShapes) {
/* 855 */       System.out.println("\n" + count++);
/* 856 */       for (String group : shape.getGroups())
/* 857 */         System.out.println("group " + group);
/* 858 */       for (String member : shape.getMembers())
/* 859 */         System.out.println("    " + member);
/*     */     }
/*     */   }
/*     */   
/*     */   private static void setupShapeTest2() throws ServiceException {
/* 864 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/* 867 */     String domainName = "test.com";
/* 868 */     Domain domain = prov.createDomain(domainName, new HashMap());
/*     */     
/* 870 */     DistributionList groupA = prov.createDistributionList("groupA@" + domainName, new HashMap());
/* 871 */     DistributionList groupB = prov.createDistributionList("groupB@" + domainName, new HashMap());
/* 872 */     DistributionList groupC = prov.createDistributionList("groupC@" + domainName, new HashMap());
/* 873 */     DistributionList groupD = prov.createDistributionList("groupD@" + domainName, new HashMap());
/*     */     
/* 875 */     String pw = "test123";
/* 876 */     Account A = prov.createAccount("A@" + domainName, pw, null);
/* 877 */     Account B = prov.createAccount("B@" + domainName, pw, null);
/* 878 */     Account C = prov.createAccount("C@" + domainName, pw, null);
/* 879 */     Account D = prov.createAccount("D@" + domainName, pw, null);
/*     */     
/*     */ 
/* 882 */     groupA.addMembers(new String[] { A.getName(), groupB.getName() });
/*     */     
/* 884 */     groupB.addMembers(new String[] { B.getName(), groupC.getName() });
/*     */     
/* 886 */     groupC.addMembers(new String[] { C.getName(), groupD.getName() });
/*     */     
/* 888 */     groupD.addMembers(new String[] { D.getName() });
/*     */   }
/*     */   
/*     */   private static void shapeTest2() throws ServiceException
/*     */   {
/* 893 */     setupShapeTest2();
/*     */     
/* 895 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/* 898 */     Set<DistributionList> groupsWithGrants = new HashSet();
/* 899 */     String domainName = "test.com";
/* 900 */     groupsWithGrants.add(prov.get(Key.DistributionListBy.name, "groupA@" + domainName));
/* 901 */     groupsWithGrants.add(prov.get(Key.DistributionListBy.name, "groupB@" + domainName));
/* 902 */     groupsWithGrants.add(prov.get(Key.DistributionListBy.name, "groupC@" + domainName));
/* 903 */     groupsWithGrants.add(prov.get(Key.DistributionListBy.name, "groupD@" + domainName));
/*     */     
/* 905 */     Set<GroupShape> accountShapes = new HashSet();
/* 906 */     Set<GroupShape> calendarResourceShapes = new HashSet();
/* 907 */     Set<GroupShape> distributionListShapes = new HashSet();
/*     */     
/* 909 */     for (DistributionList group : groupsWithGrants)
/*     */     {
/*     */ 
/* 912 */       DistributionList dl = prov.get(Key.DistributionListBy.id, group.getId());
/* 913 */       AllGroupMembers allMembers = allGroupMembers(dl);
/* 914 */       GroupShape.shapeMembers(TargetType.account, accountShapes, allMembers);
/* 915 */       GroupShape.shapeMembers(TargetType.calresource, calendarResourceShapes, allMembers);
/* 916 */       GroupShape.shapeMembers(TargetType.dl, distributionListShapes, allMembers);
/*     */     }
/*     */     
/* 919 */     int count = 1;
/* 920 */     for (GroupShape shape : accountShapes) {
/* 921 */       System.out.println("\n" + count++);
/* 922 */       for (String group : shape.getGroups())
/* 923 */         System.out.println("group " + group);
/* 924 */       for (String member : shape.getMembers()) {
/* 925 */         System.out.println("    " + member);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args)
/*     */     throws ServiceException
/*     */   {}
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/accesscontrol/CollectAllEffectiveRights.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */